﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using digitwhu.Models.IRepositorys;
using digitwhu.Models;
using digitwhu.Models.Repositorys;
using digitwhu.Helper;
using digitwhu.Logic.ClassLibrary;
using digitwhu.Logic.BusinessLogic;

namespace digitwhu.Logic.BusinessLogicControl
{
    public class BuildingLogicControl
    {
        #region 食堂相关
        public static string GetCanWindowList(string buildingId) //获取某个食堂的所有窗口列表
        {
            //canteenId:预获取所有窗口的食堂的编号
            //返回值：序列化以后的食堂窗口列表
            int canteenid = BuildingLogic.GetCanteenIdByBuildingId(Int32.Parse(buildingId));
            if (canteenid < 0)
            {
                //查询失败,输入建筑物Id不合法
                return null;
            }
            List<int> window = BuildingLogic.GetCanWindowList(canteenid);   //获取食堂窗口编号列表
            List<RCanteenWindow> result = new List<RCanteenWindow>();
            foreach (int w in window)
            {
                result.Add(GetSingleCanWindow(w));
            }
            string str = JsonHelper.JsonSerializer<List<RCanteenWindow>>(result);   //序列化对象
            return str;
        }
        public static string AddCanWindowGrade(string userId, string canWindowId, string grade)  //给某个窗口评分
        {
            //userId:要给窗口评分的用户编号
            //canWindowId:要评分的窗口编号
            //grade:用户给的分数(只能是0~5)
            //返回值：“false”表示评分失败，评分成功返回该窗口的序列化字符串
            int userid = Int32.Parse(userId);
            int canwindowid = Int32.Parse(canWindowId);
            int igrade = Int32.Parse(grade);
            bool b = BuildingLogic.AddCanWindowGrade(userid, canwindowid, igrade);
            if (b == false)
            {
                //评分成功
                return "false";
            }
            else
            {
                //评分失败
                RCanteenWindow result = GetSingleCanWindow(canwindowid);
                string str = JsonHelper.JsonSerializer<RCanteenWindow>(result);     //序列化该窗口的对象
                return str;
            }
        }
        public static string GetCanMessageList(string buildingId)//获取某个食堂的所有留言
        {
            //canteenId:预获取留言的食堂编号
            //返回值：序列化的食堂留言对象
            int canteenid = BuildingLogic.GetCanteenIdByBuildingId(Int32.Parse(buildingId));
            if (canteenid < 0)
            {
                //查询失败,输入建筑物Id不合法
                return null;
            }
            List<CanMessage> message = BuildingLogic.GetCanMessageList(canteenid);
            List<RCanMessage> result = new List<RCanMessage>();
            foreach (var m in message)
            {
                result.Add(GetSingleCanMessage(m));
            }
            string str = JsonHelper.JsonSerializer<List<RCanMessage>>(result);  //序列化对象
            return str;
        }
        public static string AddCanMessage(string userId, string buildingId, string content)//给某个食堂留言
        {
            //userId:待发表留言的用户编号
            //canteenId:预被留言的食堂编号
            //content:留言内容
            //返回值：操作失败：“false” 操作成功：新增加的留言的可序列化对象
            int userid = Int32.Parse(userId);
            int canteenid = BuildingLogic.GetCanteenIdByBuildingId(Int32.Parse(buildingId));
            if (canteenid < 0)
            {
                //查询失败,输入建筑物Id不合法
                return false.ToString();
            }
            bool b = BuildingLogic.AddCanMessage(userid, canteenid, content);
            if (b == false)
            {
                //发表留言失败
                return false.ToString();
            }
            else
            {
                //发表留言成功
                CanMessage message = BuildingLogic.GetLatestCanMessage(canteenid);
                RCanMessage result = GetSingleCanMessage(message);
                string str = JsonHelper.JsonSerializer<RCanMessage>(result);
                return str;
            }
        }
        public static string GetCanRecommendList(string buildingId)//获取给定食堂的推荐美食列表
        {
            //canteenId:预获取食堂推荐美食的食堂编号
            //返回值：推荐美食记录的对象序列化结果
            int canteenid = BuildingLogic.GetCanteenIdByBuildingId(Int32.Parse(buildingId));
            if (canteenid < 0)
            {
                //查询失败,输入建筑物Id不合法
                return null;
            }
            List<RCanRecommend> result = new List<RCanRecommend>();
            List<int> recommend = BuildingLogic.GetCanRecommendList(canteenid);
            foreach (int r in recommend)
            {
                result.Add(GetSingleCanRecommend(r));
            }
            string str = JsonHelper.JsonSerializer<List<RCanRecommend>>(result);    //序列化对象
            return str;
        }
        public static string AddCanRecommendFood(string userId, string buildingId, string contents, string picture)//给食堂推荐食物 
        {
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：成功：新推荐的推荐美食记录  推荐美食失败：false
            int userid = Int32.Parse(userId);
            int canteenid = BuildingLogic.GetCanteenIdByBuildingId(Int32.Parse(buildingId));
            if (canteenid < 0)
            {
                //查询失败,输入建筑物Id不合法
                return false.ToString();
            }
            bool b = BuildingLogic.AddCanRecommendFood(userid, canteenid, contents, picture);
            if (b == false)
            {
                return b.ToString();
            }
            RCanRecommend result = GetSingleCanRecommend(BuildingLogic.GetLatestCanRecommendFoodId(canteenid));
            //新增加的推荐美食记录
            string str = JsonHelper.JsonSerializer<RCanRecommend>(result);
            return str;
        }
        public static string DeleteCanRecommendFood(string recommendId)//删除自己曾经推荐的美食记录
        {
            //recommendId:将要删除的推荐美食记录编号
            //返回值：true：删除成功 false:删除失败
            int recommenid = Int32.Parse(recommendId);
            bool b = BuildingLogic.DeleteCanRecommendFood(recommenid);  //删除推荐
            return b.ToString();
        }
        public static string UpdateCanRecommendFood(string recommendId, string contents, string picture)//更新用户推荐的美食记录
        {
            //recommendId:推荐美食的ID
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：true：更改推荐美食成功 false:更改推荐美食失败
            int recommendid = Int32.Parse(recommendId);
            return BuildingLogic.UpdateCanRecommendFood(recommendid, contents, picture).ToString();
        }
        #region 私有函数
        private static RCanteenWindow GetSingleCanWindow(int canWindowId)  //获取指定窗口可序列化对象
        {
            //canWindowId:预获得的窗口编号
            //返回值：可序列化的窗口对象
            DigitwhuDataContext dbContext = new DigitwhuDataContext();
            RCanteenWindow window = new RCanteenWindow();
            CanShopWindow shopWindow = dbContext.CanShopWindow.Single<CanShopWindow>(w => w.id == canWindowId);
            window.canteenId = shopWindow.canteenId;
            window.canWindowId = shopWindow.id;
            window.name = shopWindow.name;
            window.grade = BuildingLogic.GetCanWindowGrade(canWindowId);
            return window;
        }
        private static RCanMessage GetSingleCanMessage(CanMessage message)  //获取某条留言可序列化的对象
        {
            RCanMessage result = new RCanMessage();
            result.canteenId = message.canteenId;
            result.contents = message.contents;
            result.dateTime = message.dateTime.ToString();
            result.userInfo = UserLogicControl.GetUserInfo(message.userId);
            return result;
        }
        private static RCanRecommend GetSingleCanRecommend(int recommendId) //获取某条美食推荐的可序列化对象
        {
            //recommendId:推荐记录的id
            //返回值：可序列化的美食推荐对象
            DigitwhuDataContext dbContext = new DigitwhuDataContext();
            CanRecommendFood recommend = dbContext.CanRecommendFood.Single<CanRecommendFood>(m => m.id == recommendId);
            RCanRecommend result = new RCanRecommend();
            result.contents = recommend.contents;
            result.dateTime = recommend.dateTime.ToString();
            result.picture = recommend.picture;
            result.recommendId = recommend.id;
            result.userInfo = UserLogicControl.GetUserInfo(recommend.userId);
            return result;
        }
        #endregion
        #endregion

        #region 寝室相关
        public static string GetAllDormStudent(string dormId)   //获取某栋寝室的所有学生信息
        {
            //dorId:寝室楼编号
            //返回值：该寝室楼的所有入住学生信息
            int dormid = Int32.Parse(dormId);
            List<int> user = BuildingLogic.GetAllDormStudent(dormid);
            List<RUserInfo> result = new List<RUserInfo>();
            foreach (int i in user)
            {
                RUserInfo temp = UserLogicControl.GetUserInfo(i);
                result.Add(temp);
            }
            string str = JsonHelper.JsonSerializer<List<RUserInfo>>(result);  //序列化对象
            return str;
        }
        #endregion

        #region 教学楼相关
        public static string GetTeachRoomHavingClass(string buildingId)//查找某栋教学楼的当前的所有上课教室
        {
            //buildingId:教学楼的编号
            //返回值：当前在上课的教室序列化后对象,按楼层划分
            DigitwhuDataContext dbContext = new DigitwhuDataContext();
            int buildingid = Int32.Parse(buildingId);
            List<int> classroom = BuildingLogic.GetTeachRoomHavingClass(buildingid);
            List<RTeachClassRoom> result = new List<RTeachClassRoom>();
            foreach (int id in classroom)
            {
                TeachClassRoom room = dbContext.TeachClassRoom.Single<TeachClassRoom>(c => c.id == id);//获取教室对象
                bool bexist = false;    //楼层对象是否已经存在
                foreach (RTeachClassRoom rteach in result)
                {
                    if (rteach.floorNumber == room.floorNumber)
                    {
                        //如果找到当前楼层的对象
                        rteach.roomList.Add(room.classRoom);        //classRoom是教室名字：如302
                        bexist = true;
                        break;      //跳出查找
                    }
                }
                if (bexist == false)
                {
                    //当前教室楼层尚未在结果集中
                    //创建新对象
                    RTeachClassRoom temp = new RTeachClassRoom();
                    temp.floorNumber = room.floorNumber;
                    temp.roomList = new List<int>();
                    temp.roomList.Add(room.classRoom);//classRoom是教室名字：如302
                    result.Add(temp);
                }
            }
            string str = JsonHelper.JsonSerializer<List<RTeachClassRoom>>(result);
            return str;
        }
        public static string GetTeachMessage(string buildingId)    //获得某栋教室的所有留言
        {
            //buildingId:预获取留言的教室的id
            //返回值：所有留言的对象集合
            int buildingid = Int32.Parse(buildingId);
            List<TeachMessage> message = BuildingLogic.GetTeachMessage(buildingid);
            List<RTeachMessage> result = new List<RTeachMessage>();
            foreach (TeachMessage m in message)
            {
                RTeachMessage temp = GetSingleTeachMessage(m);
                result.Add(temp);
            }
            string str = JsonHelper.JsonSerializer<List<RTeachMessage>>(result);
            return str;
        }
        public static string AddTeachMessage(string buildingId, string userId, string contents)//给教室留言
        {
            //buildingId:教学楼编号
            //userId：要留言的用户编号
            //contents:留言内容
            //返回值：最新的留言
            int buildingid = Int32.Parse(buildingId);
            int userid = Int32.Parse(userId);
            bool b = BuildingLogic.AddTeachMessage(buildingid, userid, contents);
            RTeachMessage result = new RTeachMessage(); //如果为空，是否可序列化？？
            if (b == true)
            {
                TeachMessage temp = BuildingLogic.GetLatestTeachMessage(buildingid);
                result = GetSingleTeachMessage(temp);
            }
            string str = JsonHelper.JsonSerializer<RTeachMessage>(result);
            return str;
        }
        #region 公用函数
        public static RTeachMessage GetSingleTeachMessage(TeachMessage message) //获得某条留言的可序列化对象
        {
            RTeachMessage temp = new RTeachMessage();
            temp.contents = message.contents;
            temp.datetime = message.dateTime.ToString();
            temp.user = UserLogicControl.GetUserInfo(message.userId);
            return temp;
        }
        #endregion
        #endregion
    }
}